package com.leecode.xiehf.page_02;

import com.leecode.Printer;

import java.util.ArrayList;
import java.util.List;

/**
 * 给定一个二叉树的根节点 root ，返回它的 中序 遍历。
 * https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
 */
public class Solution_0094 extends Printer {

    public static void main(String[] args) {
        Solution_0094 solution = new Solution_0094();
        List<Integer> list = solution.inorderTraversal(new TreeNode(1, null,
                new TreeNode(2, new TreeNode(3, null, null), null)));
        printInt(list);
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        traversal(list, root.left);
        list.add(root.val);
        traversal(list, root.right);
        return list;
    }

    private void traversal(List<Integer> list, TreeNode node) {
        if (node == null) {
            return;
        }
        traversal(list, node.left);
        list.add(node.val);
        traversal(list, node.right);
    }
}
